import time

from django.contrib import admin
from django.utils.safestring import mark_safe

import info.models
from .models import *

admin.site.site_header = '体院信息管理'
admin.site.site_title = '体院信息管理'
admin.site.index_title = '体院信息管理'


# Register your models here.
@admin.register(TAct)
class TActAdmin(admin.ModelAdmin):
    list_display = ('name', 'start_time', 'status', 'description')
    list_editable = ('status',)
    search_fields = ('name', 'start_time')
    list_filter = ('status',)
    list_per_page = 10


@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
    list_display = ('name', 'teacher_card')
    search_fields = ('name', 'teacher_card')
    list_per_page = 10


@admin.register(Trelation)
class TrelationAdmin(admin.ModelAdmin):
    list_display = ('name', 'teacher')
    search_fields = ('name', 'teacher__name')
    list_per_page = 10


@admin.register(TALog)
class TALogAdmin(admin.ModelAdmin):
    list_display = ('person', 'activity', 'place', 'create_time')
    search_fields = ('person__name',)
    list_filter = ('activity', 'place')
    list_per_page = 10

    def upload_img(self, obj):
        try:
            img = mark_safe('<img src="%s" width="200px" />' % (obj.img_url,))
        except Exception as e:
            img = ''
        return img


@admin.register(TNoLog)
class NoLog(admin.ModelAdmin):
    list_display = ("act", "teacher", "person",)
    search_fields = ('person__name',)
    list_filter = ('act__name', 'teacher')
    list_per_page = 25
    actions = ['mark_done']

    def mark_done(self, request, queryset):
        for i in queryset:
            TALog.objects.create(activity_id=i.act_id, person_id=i.person_id)

    mark_done.short_description = '标记已完成'


@admin.register(Place)
class PlaceAdmin(admin.ModelAdmin):
    list_display = ('name',)
